package com.hqyj.javaSpringCloudTest.dao;

import com.hqyj.entity.test.Country;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface CountryDao {
    /**
     * 联合查询接口
     * @Results 注解用于将国家下的城市列表查询出来，并封装到cities
     * value中写的是映射关系
     * dao层通过注解设置Result结果集，进行连调查询
     */
    @Select("select * from test_country where country_id=#{countryId}")
    @Results(id = "countryResults", value = {
            @Result(column = "country_id", property = "countryId"),
            @Result(column = "country_id",
                    property = "cities",
                    javaType = List.class,
                    many = @Many(select = "com.hqyj.javaSpringCloudTest.dao.CityDao.getCitiesByCountryId")
            )
    })
    Country getCountryByCountryId(int countryId);

    @Select("select * from test_country where country_name=#{countryName} " +
            "and local_country_name=#{localCountryName}")
    @ResultMap(value = "countryResults")
    Country getCountryByCountryName(@Param("countryName") String countryName,
                                    @Param("localCountryName") String localCountryName);
}
